home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QuickDraw GX / GX->PostScript Sample / GXToPostScript / I⁄O Utilities / IOUtilities.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  3.5 KB  |  156 lines  |  [TEXT/CWIE]

  1. /*
  2.      File:        IOUtilities.cp
  3.  
  4.      Contains:    QuickDraw GX to PostScript conversion code.
  5.  
  6.      Version:    Technology:    Quickdraw GX 1.1.x
  7.       
  8.      Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. /*********************************
  12.     Routine:        HexBlockMove
  13.     
  14.     Like BlockMoveData, only convert to hex in process.
  15.     
  16.     srcStr:            Pointer to source data to move.
  17.     dstPtr:            Pointer to destination (must be 2x in size of source)
  18.     num:            How many bytes to convert and move
  19.     
  20. ***********************************/
  21. #include <Errors.h>
  22. #include <Resources.h>
  23. #include <Memory.h>
  24. #include <String.h>
  25. #include <Script.h>
  26. #include "IOUtilities.h"
  27.  
  28. #define HiNybble(x) ( x >> hiNybShift )
  29. #define LoNybble(x) ( x & loNybMask )
  30. void HexBlockMove(unsigned char *srcStr, unsigned char *dstPtr, unsigned long num)
  31.     {
  32.         const char *hexDigits =        "0123456789ABCDEF";        //Generate pc relative reference to string.
  33.         register long hexLong = (long)hexDigits;                            //do this so address of string is in data register.
  34.         register unsigned char hiNybShift =    0x04;                        //constants in registers for speedy operation.
  35.         register unsigned char loNybMask=        0x0F;                    //constants in registers for speedy operation.
  36.  
  37.         if (num > 0) {        
  38.         
  39.             do {
  40.                 
  41.                 *dstPtr++ = *(Ptr)( hexLong + HiNybble(*srcStr) );
  42.                 *dstPtr++ = *(Ptr)( hexLong + LoNybble(*srcStr) );
  43.                 ++srcStr;
  44.             
  45.             } while (--num);
  46.             
  47.         }//end if
  48.     
  49.  
  50.     }//HexBlockMove
  51.  
  52.  
  53. /******
  54.     Wrapper for NewHandle
  55.     that returns an error.
  56. ******/
  57. OSErr        PrNewHandle(Handle *h, long size)
  58. {
  59.     OSErr        status;
  60.     *h = NewHandle(size);
  61.     status = MemError();
  62.     return status;
  63. }
  64.  
  65. OSErr    PrNewHandleClear(Handle *h, long size)
  66. {
  67.     OSErr    status = PrNewHandle(h, size);
  68.     if (status == noErr)
  69.         memset(**h, 0, size);
  70.         
  71.     return status;
  72. }
  73.  
  74. OSErr    PrSetHandleSize(Handle h, long newSize)
  75. {
  76.     OSErr        status;
  77.     SetHandleSize(h, newSize);
  78.     status = MemError();
  79.     return status;
  80. }
  81.  
  82.  
  83. /**
  84.     Wrapper for GetResource that returns an error.
  85. **/
  86. OSErr    FetchResource(OSType    resType, short ID, Handle *h)
  87. {
  88.     OSErr        status;
  89.     *h = GetResource(resType, ID);
  90.     if (*h != nil) {
  91.         status = noErr;
  92.     } else {
  93.         status = ResError();
  94.         if (status == noErr)
  95.             status = MemError();
  96.             if (status == noErr)
  97.                 status = resNotFound;
  98.     }//end if
  99.     
  100.     return status;
  101.  
  102. }
  103.  
  104. /**
  105.     Wrapper for NewPtr that returns an error
  106. **/
  107. OSErr    PrNewPtr(Ptr *p, long size)
  108. {
  109.     OSErr    status;
  110.     *p = NewPtr(size);
  111.     status = MemError();
  112.     return status;
  113. }
  114.  
  115.  
  116. /*******************************************************
  117.     Function: MakeMac8BitEncoding:
  118.     
  119.     Function sets up an encoding vector for the standard
  120.     Mac 8 bit encoding for the current active script system.
  121.     
  122.     theFont:            the font to make encoding for.
  123.     encoding:        array of 256 shorts, encoding will be returned
  124.                         in here.
  125.                         
  126.     returns:            The number of bytes encoded. (should always be 256)
  127.     
  128. ********************************************************/
  129. long MakeMac8BitEncoding(gxFont theFont, unsigned short encoding[])
  130.     {
  131.         gxFontScript            theScript;
  132.         long                        index;
  133.         unsigned char            theBytes[256];
  134.         unsigned char            *aByte;
  135.         long                        length;
  136.         short                        i;
  137.     
  138.         theScript = (gxFontScript)GetScriptManagerVariable(smSysScript) + 1;        // gx graphic scripts are 1+scriptmgr scripts.
  139.  
  140.         if( ! ( index = GXFindFontEncoding(theFont, gxMacintoshPlatform, theScript, gxNoLanguage) ) )
  141.             {
  142.                 index = GXFindFontEncoding(theFont, gxMacintoshPlatform, gxRomanScript, gxNoLanguage);
  143.             }
  144.             
  145.         
  146.         aByte = theBytes;
  147.         for (i = 0; i < 256; ++i)
  148.             *aByte++ = i;
  149.         
  150.         length = GXApplyFontEncoding(theFont, index, nil, theBytes, 256, encoding, nil);
  151.         
  152.         return(length);
  153.     
  154.     }//MakeMac8BitEncoding
  155.  
  156.